package cn.niit.service.impl;

import cn.niit.dao.SysResourceMapper;
import cn.niit.dao.SysUserMapper;
import cn.niit.entity.SysResource;
import cn.niit.entity.SysUser;
import cn.niit.service.SysResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service("sysResourceService")
public class SysResourceServiceImpl implements SysResourceService {
    //添加Dao层的引用
    @Resource
    private SysResourceMapper sysResourceMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public Set<String> getPermissionsByUserCode(String userCode) {
        SysUser user = sysUserMapper.getByUserCode(userCode);
        if (user == null) {
            return Collections.emptySet();
        }
        Set<String> set = new HashSet<String>();
        List<String> list = sysResourceMapper.listUserPermissions(user.getId());
        set.addAll(list);
        return set;
    }

    /**
     * 根据用户Id 查询该用户可访问的菜单
     *
     * @param userId
     */
    @Override
    public List<SysResource> getMenuListByUserId(Long userId) {
        return sysResourceMapper.getMenuListByUserId(userId);
    }

    /**
     * 根据用户的id查询用户可以访问的按钮
     *
     * @param userId
     * @return
     */
    @Override
    public List<SysResource> getButtonListByUserId(Long userId) {
        return sysResourceMapper.getButtonListByUserId(userId);
    }
}
